package com.captain.leetcode.链表;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/3/19 15:07
 */
public class 移除链表元素203 {

    public static void main(String[] args) {
        ListNode listNode = new 移除链表元素203().removeElements(ListNode.getInstance(), 2);

    }
    public ListNode removeElements(ListNode head, int val) {
        if (head == null) return null;
        ListNode dummy = new ListNode(-1, head);
        ListNode pre = dummy;
        ListNode cur = head;
        //循环遍历链表,查看是否存在相同的值
        while (cur != null) {
            if (cur.val == val) {
                pre.next = cur.next;
            } else {
                pre = cur;
            }
            cur = cur.next;
        }
        return dummy.next;
    }
}
